package top.goldenyear.porpoise.framework.web.core.generator.config;

import cn.hutool.v7.core.collection.CollUtil;
import java.util.HashMap;
import java.util.Map;
import lombok.*;
import lombok.experimental.Accessors;

/**
 * 表的单独设置
 *
 * @author airhead
 */
@Data
@Accessors(chain = true)
public class TableConfig {
  private String domain;

  /** 表名 */
  private String tableName;

  /** 数据库的 schema（模式） */
  private String schema;

  /** 默认为 驼峰属性 转换为 下划线字段 */
  private Boolean camelToUnderline;

  /** 对应列的配置 */
  private Map<String, ColumnConfig> columnConfigMap;

  /** 需要生成的模块 */
  private Map<String, Boolean> generateConfigs;

  /** 模版类型 1-单表，2-树表，10-主表-普通，11-主表ERP，12-主表树表，20-子表 */
  private Integer templateType = 1;

  /** 主表 */
  private String masterTable;

  /** 子表连接字段 */
  private String joinColumn;

  /** 主子表关系 0-1:1，1-1:n */
  private Integer relation;

  /** 树表父字段 id */
  private String parentColumnId;

  /** 树表父字段名称 */
  private String parentColumnName;

  // 表格配置

  private Boolean tableEditable = false;
  private String tableTitle;
  private Boolean tablePaginate;
  private Integer tablePageSize;
  private String tableWidth;
  private String tableHeight;
  private Boolean tableAdjustWidth;
  private Boolean tableCopyToExcel;
  private String tableLeftFix;
  private String tableRightFix;
  private String tableSortField;
  private String tableSortMethod;
  private Boolean enableImport = false;
  private Boolean enableExport = false;
  private Boolean enableAdd = true;
  private Boolean enableEdit = true;
  private Boolean enableDel = true;
  private Boolean enableRefresh = true;
  private Boolean enableInitSort = true;
  private Boolean enableCheck = true;

  // from和drawer

  private Boolean drawer = true;
  private Integer formColCount;
  private String formColProportion;
  private String formWidth = "360";
  private String formHeight;
  private String formElementStyle;
  private String formElementProportion;

  // cache

  private Boolean enableCache = false;

  // controller

  private Boolean enableSimpleList = false;
  private Boolean enableOptions = false;

  // 菜单

  private Integer parentMenuId;

  public TableConfig() {
    generateConfigs = new HashMap<>(0);
    columnConfigMap = new HashMap<>(0);
  }

  public static TableConfig of() {
    return new TableConfig();
  }

  public TableConfig addColumnConfig(ColumnConfig columnConfig) {
    columnConfigMap.put(columnConfig.getColumnName(), columnConfig);

    return this;
  }

  public ColumnConfig getColumnConfig(String columnName) {
    return CollUtil.isEmpty(columnConfigMap) ? null : columnConfigMap.get(columnName);
  }

  public TableConfig addGenerateConfig(String module, Boolean generate) {
    generateConfigs.put(module, generate);

    return this;
  }

  public Boolean getGenerateConfig(String module) {
    return CollUtil.isNotEmpty(generateConfigs) && generateConfigs.get(module);
  }
}
